//
//  LWComposeView.swift
//  Microblog
//
//  Created by Roman on 17/1/13.
//  Copyright © 2017年 Roman. All rights reserved.
//

import UIKit
import pop


class LWComposeView: UIView {

    
    var belongVC: UIViewController?
    //定义一个容器视图来进行按钮的添加,方便做动画
    lazy var buttons:[LWComposeButton] = [LWComposeButton]()
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        backgroundColor = UIColor.red
        setupUI()
    }
    
    ///显示控制器
    func show(targetVC: UIViewController?)  {
        self.belongVC = targetVC
        targetVC?.view.addSubview(self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        let view = UIImageView(image: UIImage.snapshotCurrentScreen())
        
        self.addSubview(view)
        //高斯模糊
        let toolBar = UIToolbar(frame: UIScreen.main.bounds)
        self.addSubview(toolBar)
        
        //添加广告视图
        let sloganIv = UIImageView(image: #imageLiteral(resourceName: "compose_slogan"))
        self.addSubview(sloganIv)
        //设置约束
        sloganIv.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(100)
        }
        
        addChildButtons()
    }
    
    //添加按钮
    private func addChildButtons() {
        //调用构造方法获取的是自定义按钮
        let btnMargin = (ScreenWidth - 3 * composeBtnW) / 4
        let images = [#imageLiteral(resourceName: "tabbar_compose_lbs"),#imageLiteral(resourceName: "tabbar_compose_idea"),#imageLiteral(resourceName: "tabbar_compose_photo"),#imageLiteral(resourceName: "tabbar_compose_music"),#imageLiteral(resourceName: "tabbar_compose_friend"),#imageLiteral(resourceName: "tabbar_compose_more")]
        
        
        for i in 0..<images.count {
            //获取行列来计算按钮的位置
            let row =  i / 3
            //表示去每行第几个元素
            let col = i % 3
            //下面表示列
            let BtnX = btnMargin + (composeBtnW + btnMargin) * CGFloat(col)
            let BtnY = (btnMargin + composeBtnH) * CGFloat(row) + ScreenHeight
            
            let btn = LWComposeButton()
            //设置文字和图片
            let image = images[i]
            btn.setImage(image, for: .normal)
            btn.setTitle("交友", for: .normal)
            btn.frame = CGRect(x: BtnX, y: BtnY, width: composeBtnW, height: composeBtnH)
            //btn.sizeToFit()
            self.addSubview(btn)
            //添加到容器视图中取
            self.buttons.append(btn)
            btn.addTarget(self, action: #selector(btnClick(btn:)), for: .touchUpInside)
        }
    }
    @objc private func btnClick(btn: LWComposeButton) {
        //被点击的按钮放大
        //其他按钮缩小
            UIView.animate(withDuration: 0.5, animations: {
                for composeBtn in self.buttons {
                    
                    composeBtn.alpha = 0.1
                    if composeBtn == btn{
                        //被点击的按钮
                        composeBtn.transform = CGAffineTransform(scaleX: 2, y: 2)
                    }else{
                        composeBtn.transform = CGAffineTransform(scaleX: 0.2, y: 0.2)
                    }
                }
        }) { (_) in
            //print("ok")
            //进行页面的跳转,一般都是根据固定的字符串进行跳转
            //1.根据字符串来找到对应的类型
            let classStr = "Microblog.LWComposeViewController"
            let classType = NSClassFromString(classStr) as! UIViewController.Type
            //2.实例化控制器对象
            let composeVC = classType.init()
            let nav = UINavigationController(rootViewController: composeVC)
            //3.跳转到对应的控制器
            //view中没有present方法
            self.belongVC?.present(nav, animated: true, completion: nil)
            
        }
    
        
        
        
    }
    
    
    override func willMove(toSuperview newSuperview: UIView?) {
        //开始做动画效果
        /*
        POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        
        CGPoint point = [tap locationInView:self.view];
        
        springAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, point.x, point.y)];
        
        
        //弹性值
        springAnimation.springBounciness = 20.0;
        //弹性速度
        springAnimation.springSpeed = 20.0;
        
        [_springView pop_addAnimation:springAnimation forKey:@"changeframe"];
 */
        //添加6个按钮开始做动画
        for (offset,btn) in buttons.enumerated() {
           self.showButtonAnimation(btn: btn, offset: offset)
            
            
        }
       
        
    }
    //移除
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for (offset,btn) in buttons.reversed().enumerated() {
            
            self.showButtonAnimation(btn: btn, offset: offset, isshow: false)
        }
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3, execute: {
            self.removeFromSuperview()
        })
    }
    //进行封装
    private func showButtonAnimation(btn: LWComposeButton,offset: Int, isshow: Bool = true) {
        
        let springAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        springAnimation?.springBounciness = 10
        springAnimation?.springSpeed = 10
        //设置动画延迟时间
        springAnimation?.beginTime = CACurrentMediaTime() + 0.25 * Double(offset)
        springAnimation?.toValue = CGPoint(x: btn.center.x, y: btn.center.y + (isshow ? -350 : 350))
        btn.pop_add(springAnimation, forKey: nil)
        
    }
    
    
    
    
    
    
    
    
    
}
